package com.tech.connect.zhaofuwu;

import android.content.Context;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.ksy.common.utils.BaseAdapter;
import com.ksy.common.utils.BaseHolder;
import com.tech.connect.R;
import com.tech.connect.api.UserInfo;
import com.tech.connect.model.CallBack;
import com.tech.connect.model.ItemLvXin;
import com.tech.connect.view.IndexImageLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class LvYouView extends NestedScrollView {

    public Context context;
    public IndexImageLayout viewPager;
    public ImageView ivPraise;
    public TextView tvPraise;
    public LinearLayout llFavorite;
    public ImageView ivFavorite;
    public TextView tvFavorite;
    public LinearLayout llPraise;
    public CallBack callBack;
    public ItemLvXin item;
    public TextView tvName;
    public ImageView ivSex;
    public TextView tvAge;
    public TextView tvAddress;
    public TextView tvHeight;
    public TextView tvWeight;
    public TextView tvStudy;
    public TextView tvMarry;
    public RecyclerView recycler1;
    public TextView tvMoney;
    public TextView tvNote;
    public Button btEnter;
    public TextView tvView;
    public TextView tvDynamic;
    public LinearLayout llDynamic;
    public RatingBar ratingBar;
    public RecyclerView recycler2;
    public List<String> dynamicImageList = new ArrayList<>();
    public BaseAdapter<String, BaseHolder> adapter1;
    public BaseAdapter<String, BaseHolder> adapter2;
    public OnClickListener clickYueDanListener;
    public OnClickListener clickZanListener;

    public LvYouView(Context context) {
        this(context, null);
    }

    public LvYouView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_lvyou, this, true);
        initView();
    }

    private void initView() {
        viewPager = findViewById(R.id.viewPager);

        llPraise = findViewById(R.id.llPraise);
        llPraise.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickZanListener != null) {
                    clickZanListener.onClick(v);
                }
            }
        });
        ivPraise = findViewById(R.id.ivPraise);
        tvPraise = findViewById(R.id.tvPraise);
        llFavorite = findViewById(R.id.llFavorite);
        ivFavorite = findViewById(R.id.ivFavorite);
        tvFavorite = findViewById(R.id.tvFavorite);

        tvName = findViewById(R.id.tvName);
        ivSex = findViewById(R.id.ivSex);
        tvAge = findViewById(R.id.tvAge);
        tvAddress = findViewById(R.id.tvAddress);
        tvHeight = findViewById(R.id.tvHeight);
        tvWeight = findViewById(R.id.tvWeight);
        tvStudy = findViewById(R.id.tvStudy);
        tvMarry = findViewById(R.id.tvMarry);

        llDynamic = findViewById(R.id.llDynamic);
        tvDynamic = findViewById(R.id.tvDynamic);
        recycler1 = findViewById(R.id.recycler1);
        recycler1.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));

        tvMoney = findViewById(R.id.tvMoney);
        tvNote = findViewById(R.id.tvNote);
        btEnter = findViewById(R.id.btEnter);
        btEnter.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickYueDanListener != null) {
                    clickYueDanListener.onClick(v);
                }
            }
        });
        tvView = findViewById(R.id.tvView);

        ratingBar = findViewById(R.id.ratingBar);

        recycler2 = findViewById(R.id.recycler2);
        viewPager.updateImages(null, 0);
    }

    public void update(ItemLvXin itemLvXin, CallBack callBack) {
        if (callBack != null) {
            callBack.onStart();
        }
        this.callBack = callBack;
        this.item = itemLvXin;
        String images = item.images;
        List<String> imageList = new ArrayList<>();
        if (TextUtils.isEmpty(images)) {

        } else if (images.contains(";")) {
            String[] split = images.split(";");
            List<String> strings = Arrays.asList(split);
            imageList.addAll(strings);
        } else {
            imageList.add(images);
        }
        if (imageList.isEmpty()) {
            viewPager.setVisibility(INVISIBLE);
        } else {
            viewPager.setVisibility(VISIBLE);
        }
        viewPager.updateImages(imageList, 0);
        tvPraise.setText(String.valueOf(item.likeCount));
        UserInfo user = item.user;
        if (user != null) {
            tvName.setText(user.nickName);
            if (user.gender == 0) {
                ivSex.setImageResource(R.drawable.icon_women);
            } else {
                ivSex.setImageResource(R.drawable.icon_man);
            }
            tvAge.setText(String.valueOf(user.age));
            tvAddress.setText(user.cityName);
            tvHeight.setText(user.height + "cm");
            tvWeight.setText(user.weight + "kg");
            tvStudy.setText(user.educationName);
            tvMarry.setText(user.marriageName );
        } else {
            tvName.setText("");
            tvAge.setText("");
            tvAddress.setText("");
            tvHeight.setText("");
            tvWeight.setText("");
            tvStudy.setText("");
            tvMarry.setText("");
        }
        dynamicImageList.clear();
        if (item.serviceCircleList != null) {
            dynamicImageList.addAll(item.serviceCircleList);
        }
        if (dynamicImageList.isEmpty()) {
            llDynamic.setVisibility(GONE);
        } else {
            llDynamic.setVisibility(VISIBLE);
        }
        initAdapter1();
//
        Spanned money = Html.fromHtml("<font color='#f49930'>" + item.price + "</font>" + "<font color='#666666'>" + item.priceUnit + "</font>");
        tvMoney.setText(money);
        tvNote.setText(item.introduce);
        tvView.setText("浏览" + String.valueOf(itemLvXin.browseTimes) + "次");
        ratingBar.setIsIndicator(true);
        ratingBar.setMax(5);
        ratingBar.setStepSize(0.5f);
        ratingBar.setRating(item.score);
        if (callBack != null) {
            callBack.onFinish();
        }
    }

    private void initAdapter1() {
        if (adapter1 == null) {
            adapter1 = new BaseAdapter<String, BaseHolder>(R.layout.item_layout_image, dynamicImageList) {
                @Override
                protected void convert(BaseHolder helper, int position) {
                    View iv = helper.getView(R.id.iv);
                }
            };
            recycler1.setAdapter(adapter1);
        }
        adapter1.notifyDataSetChanged();
    }

    private void initAdapter2() {
        if (adapter2 == null) {
            adapter2 = new BaseAdapter<String, BaseHolder>(R.layout.item_layout_image, dynamicImageList) {
                @Override
                protected void convert(BaseHolder helper, int position) {

                }
            };
            recycler2.setAdapter(adapter2);
        }
        adapter2.notifyDataSetChanged();
    }

    public void setYueDanClickListener(OnClickListener clickListener) {
        this.clickYueDanListener = clickListener;
    }

    public void setZanClickListener(OnClickListener zanClickListener) {
        this.clickZanListener = zanClickListener;
    }
}
